package com.jack.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import com.jack.entity.Student;
import com.jack.service.StudentService;
import com.jack.spider.QiuBaiPageProcessor;
import com.jack.spider.jsoup.JsoupParseHtml;

@RestController
@RequestMapping("/magic")
public class WebMagicController implements Pipeline{
	@Autowired
	private StudentService	studentService;

	@ResponseBody
	@RequestMapping("/find/{id}")
	public Student find(@PathVariable("id") Long id) {
		Student student = studentService.get(id);
		return student;
	}
	
	@RequestMapping("/jsoup")
	public void jsoupRun() {
		List<String> contents =JsoupParseHtml.fetchContent(5);
		saveStudent(contents);
	}
	@RequestMapping("/run")
	public void run() {
		testSave();//输出 false
		Spider.create(new QiuBaiPageProcessor()).addUrl("http://www.qiushibaike.com/textnew/")
		// 开启5个线程抓取
				.thread(5).addPipeline(new WebMagicController())
				// 启动爬虫
				.run();
	}

	@Override
	public void process(ResultItems resultItems, Task task) {
		List<String> ht_l = resultItems.get("list");
		saveStudent(ht_l);//输出为 true,报出 NullPointerException;
	}
	public void testSave(){
		System.err.println(studentService==null);
	}
	public void saveStudent(List<String> ht_l ) {
		List<Student> lists=new ArrayList<Student>();
		for (String string : ht_l) {
			Student student=new Student();
			student.setName(string);
			lists.add(student);
		}
		studentService.saveAll(lists);//这里的 studentService 为空
	}
}
